<!-- Horizontal Form -->
 <div class="box box-info">
      <div class="box-header with-border">
        <h3 class="box-title">添加商品</h3>
        <h3 class="msg"></h3>
      </div>
      <!-- /.box-header -->
      <!-- form start -->
      <form class="form-horizontal">
        <div class="box-body">
         
          <div class="form-group">
            <label for="goodsnameId" class="col-sm-2 control-label">商品名</label>
            <div class="col-sm-10">
              <input type="text" name="goodsname" class="form-control" id="goodsnameId">
            </div>
          </div>

          <div class="form-group">
            <label for="originalPriceId" class="col-sm-2 control-label">原价</label>
            <div class="col-sm-10">
              <input type="text" name="originalPrice" class="form-control"  id="originalPriceId">
            </div>
          </div>
          
          <div class="form-group">
            <label for="priceId" class="col-sm-2 control-label">现价</label>
            <div class="col-sm-10">
              <input type="text" name="price" class="form-control"  id="priceId">
            </div>
          </div>
          
          <div class="form-group">
            <label for="addrId" class="col-sm-2 control-label">产地</label>
            <div class="col-sm-10">
              <input type="text" name="addr" class="form-control"  id="addrId">
            </div>
          </div>

          <div class="form-group">
            <label for="stateId" class="col-sm-2 control-label">状态</label>
            <div class="col-sm-10" id="stateId">
              <input type="radio" name="state" value="0">下架
              <input type="radio" name="state" value="1">上价
            </div>
          </div>

          <div class="form-group">
            <label for="typeId" class="col-sm-2 control-label">类型</label>
            <div class="col-sm-10" id="typeId">
            </div>
          </div>

        </div>
        <!-- /.box-body -->
        <div class="box-footer">
          <button type="button" class="btn btn-default btn-cancel">Cancel</button>
          <button type="button" class="btn btn-info pull-right btn-save">Save</button>
        </div>
        <!-- /.box-footer -->
      </form>
    </div>
<script type="text/javascript" src="bower_components/layer/layer.js"></script>
<script type="text/javascript">

 $(document).ready(function(){
	 //页面加载完成以后加载角色信息并初始化页面
	  doLoadRoles();
	 //事件注册
	 $(".box-footer")
	 .on("click",".btn-cancel",doCancel)
	 .on("click",".btn-save",doSaveOrUpdate);
	 
	  $("#treeLayer")
	  .on("click",".btn-cancel",doHideTree)
	  .on("click",".btn-confirm",doConfirm);
	 
	 $(".form-horizontal")
	 .on("click",".load-sys-dept",doLoadZTreeNodes);
 });

 //确定按钮
 function doConfirm(){
	  //1.获取选中的记录(id,name);
	  var selectedNodes=zTree.getSelectedNodes();
	  var node=selectedNodes[0];
	  //2.将id和name填写或绑定在具体对象上
	  $("#deptId").val(node.name);
	  console.log("node.id="+node.id)
	  $("#deptId").data("deptId",node.id)
 }
 

 function doCancel(){
	$("#mainContentId").removeData("rowData");
	$("#mainContentId").load("goods/goodles_list");
 }
 function doSaveOrUpdate(){
	 var rowData=$("#mainContentId").data("rowData");
	 //1.获取表单数据
	 var params=doGetEditFormData();
	 if(rowData)params.id=rowData.id;
	 //2.发起异步请求
	 var insertUrl="goods/doSaveObject";
	 var updateUrl="goods/doUpdateObject";
	 var url=rowData?updateUrl:insertUrl;
	 console.log(params);
	 $.post(url,params,function(result){
		 if(result.state==1){
			 alert(result.message);
			 doCancel();
		 }else{
			alert(result.message);
		 }
	 })
 }
 function doGetEditFormData(){
	 //获取用户输入的数据
	 var params={
        name:$("#goodsnameId").val(),
        originalPrice:$("#originalPriceId").val(),
	    price:$("#priceId").val(),
        addr:$("#addrId").val(),
        state:$("#stateId input[name='state']:checked").val(),
        goodsTypeId:$("#typeId input[name='typeId']:checked").val()
	 }
	 return params;
 }
 function doLoadRoles(){
	 var url="goods/doFindType"
	 $.getJSON(url,function(result){
		 if(result.state==1){
		  doInitPageRoles(result.data);
		  doInitFormData();//修改时
		 }else{
		  alert(result.message);
		 }
	 })
 }
 function doInitFormData(){
     var data=$("#mainContentId").data("rowData");
     if(!data)return;
	 console.log(data);
	 //初始化用户信息
	 $("#goodsnameId").val(data.name);
	 $("#originalPriceId").val(data.originalPrice);
	 $("#priceId").val(data.price);
	 $("#addrId").val(data.addr);
     $("#stateId input[value="+data.state+"]").prop("checked",true);
     $("#typeId input[value='"+data.goodsTypeId+"']").prop("checked",true);

	 //初始化用户角色信息
	 // var ids=data.roleIds;
	 // for(var i in ids){
	 //  $("#rolesId input[value='"+ids[i]+"']")
	 //  .prop("checked",true);
	 // }
 }
function doInitPageRoles(data){//List<CheckBox>
	 //1.获取角色要显示的位置对象
	 var div=$("#typeId");
	 div.empty();
	 //2.迭代数据，将数据追加到div
	 var input="<input type='radio' name='typeId' value='[id]'>[name]"
	 for(var i in data){
		 //记住每次replace时都会产生一个新的字符串对象
		 var newInput=
		 input.replace("[id]",data[i].id)
		 .replace("[name]",data[i].typeName);
		 div.append(newInput)
	 }
 }
</script>
    
    
    
    
    
    
    